<?php

/***

LZRoster: A roster for World of Warcraft.
Copyright (C) 2007  Emmanuel Cron

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

***/

require_once "class/Load.class.php";
require_once "class/Sql.class.php";
require_once "class/StatusRoster.class.php";
require_once "class/Inputs.class.php";
require_once "class/Tooltip.class.php";
require_once "class/Config.class.php";
require_once "class/Localization.class.php";
require_once "class/LocaleContainer.class.php";
require_once "class/SessionRoster.class.php";
require_once "class/SiteContainer.class.php";
require_once "class/Revision.class.php";
require_once "class/RevisionManager.class.php";

$load = Load::GetInstance();
$inputs = Inputs::GetInstance();
$config = Config::GetInstance();
$tooltip = Tooltip::GetInstance();
$localization = Localization::GetInstance();
$locale_container = LocaleContainer::GetInstance();
$site_container = SiteContainer::GetInstance();
$session_roster = SessionRoster::GetInstance();
$revision = Revision::GetInstance();

if ($inputs->HasGetData('reset'))
{
	$session_roster->DelVar('sqlServer');
	$session_roster->DelVar('sqlBase');
	$session_roster->DelVar('sqlUser');
	$session_roster->DelVar('sqlCreate');
	
	$session_roster->DelVar('rosterTitle');
	$session_roster->DelVar('rosterGuild');
	$session_roster->DelVar('rosterServer');
	
	$session_roster->DelVar('rosterLanguage');
	
	$session_roster->DelVar('rosterBanner');
	
	$session_roster->DelVar('rosterArmory');
	$session_roster->DelVar('rosterItemDatabase');
	$session_roster->DelVar('rosterSpellDatabase');
	
	$session_roster->Redirect("install.php");
}

if ($inputs->HasPostData('install'))
{
	$status_roster = StatusRoster::GetInstance();
	
	$session_roster->SaveVar('sqlServer', $inputs->Post('sqlServer'));
	$session_roster->SaveVar('sqlBase', $inputs->Post('sqlBase'));
	$session_roster->SaveVar('sqlUser', $inputs->Post('sqlUser'));
	$session_roster->SaveVar('sqlCreate', $inputs->Post('sqlCreate'));
	
	$session_roster->SaveVar('rosterTitle', $inputs->Post('rosterTitle'));
	$session_roster->SaveVar('rosterGuild', $inputs->Post('rosterGuild'));
	$session_roster->SaveVar('rosterServer', $inputs->Post('rosterServer'));
	
	$session_roster->SaveVar('rosterLanguage', $inputs->Post('rosterLanguage'));
	
	$session_roster->SaveVar('rosterBanner', $inputs->Post('rosterBanner'));
	
	$session_roster->SaveVar('rosterArmory', $inputs->Post('rosterArmory'));
	$session_roster->SaveVar('rosterItemDatabase', $inputs->Post('rosterItemDatabase'));
	$session_roster->SaveVar('rosterSpellDatabase', $inputs->Post('rosterSpellDatabase'));
	
	if ( $config->SetLocalization($inputs->Post('rosterLanguage')) == false )
	{
		$status_roster->AddError($localization->Get('this localization does not exist'));
	}
	else
	{
		$session_roster->SetLocalization($inputs->Post('rosterLanguage'));
		$localization->UpdateToSessionLocale();		
	}
	
	$status_roster->AddNote($localization->Get('analysing configuration'));
	
	if ( strlen($inputs->Post('adminPassword')) < 1 or strlen($inputs->Post('rosterPass')) < 1 )
	{
		$status_roster->AddError($localization->Get('the admin password or the user password is missing'));
	}
	
	$config->SetAdminPassword($inputs->Post('adminPassword'));
	
	$config->SetSqlServer($inputs->Post('sqlServer'));
	$config->SetSqlDatabase($inputs->Post('sqlBase'));
	$config->SetSqlUser($inputs->Post('sqlUser'));
	$config->SetSqlPassword($inputs->Post('sqlPass'));
	
	$config->SetRosterTitle($inputs->Post('rosterTitle'));
	$config->SetRosterServer($inputs->Post('rosterServer'));
	$config->SetRosterPassword($inputs->Post('rosterPass'));
	$config->SetRosterBanner($inputs->Post('rosterBanner'));	
	
	if ( $config->SetRosterGuilds($inputs->Post('rosterGuild')) == false )
	{
		$status_roster->AddError($localization->Get('could not save guild information correctly. please try again'));
	}
	
	$config->SetWoWArmory($inputs->Post('rosterArmory'));
	$config->SetItemDatabase($inputs->Post('rosterItemDatabase'));
	$config->SetSpellDatabase($inputs->Post('rosterSpellDatabase'));
	
	if ($status_roster->HasError())
	{
		$session_roster->SaveStatus($status_roster);
		$session_roster->Redirect("install.php");
	}
	
	$status_roster->AddNote($localization->Get('testing sql connection'));
	
	if ( Sql::HasInstance() )
	{
		$status_roster->AddError($localization->Get('sql has been initialized before installation. cannot continue'));
	}
	elseif ( Sql::Test($inputs->Post('sqlServer'), $inputs->Post('sqlBase'), $inputs->Post('sqlUser'), $inputs->Post('sqlPass')) == false)
	{
		$status_roster->AddError($localization->Get('could not connect to the database. please check info'));
	}
	
	if ($status_roster->HasError())
	{
		$session_roster->SaveStatus($status_roster);
		$session_roster->Redirect("install.php");
	}
	else
	{
		$status_roster->AddNote($localization->Get('configuration seems to be valid. trying to save it'));
	}
	
	if ($config->Save() == false)
	{
		$status_roster->AddError($localization->Get('could not save the configuration. check your rights on the class.data folder'));
		
		$session_roster->SaveStatus($status_roster);
		$session_roster->Redirect("install.php");
	}
	else
	{
		$status_roster->AddSuccess($localization->Get('configuration saved, connecting to the database'));
	}
	
	if ($inputs->HasPostData('sqlCreate') or $revision->GetInstalledRevision() == 0)
	{
		$sql = Sql::GetInstance(); // Stops the script if it fails
		$revision_manager = RevisionManager::GetInstance();
		
		$status_roster->AddNote($localization->Get('deleting old tables (no confirmation)'));
		
		if ($revision_manager->DeleteDb() == false)
		{
			$status_roster->AddError($localization->Get('could not delete the current tables'));
		
			$session_roster->SaveStatus($status_roster);
			$session_roster->Redirect("install.php");
		}
		
		$status_roster->AddNote($localization->Get('creating/recreating tables'));
		
		if ($revision_manager->CreateDb() == false)
		{
			$status_roster->AddError($localization->Get('did not correctly create the tables'));
		
			$session_roster->SaveStatus($status_roster);
			$session_roster->Redirect("install.php");
		}
	}
	
	$status_roster->AddSuccess($localization->Get('installation successful. please remove the installation script to complete the process'));
	$status_roster->AddNote($localization->Get('you will then be able to go on the %s home page', "index.php"));
	
	$session_roster->SaveStatus($status_roster);
	$session_roster->Redirect("install.php?reset");
}

if ( !is_writable("class.data") )
{
	$status_roster = StatusRoster::GetInstance();
	
	$status_roster->AddError( $localization->Get('the class.data folder is not writable. please make it writable') );
}

if ( !is_writable("logs") )
{
	$status_roster = StatusRoster::GetInstance();
	
	$status_roster->AddError( $localization->Get('the log folder is not writable. please make it writable') );
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr">
	<head>
		<title>LZRoster<?php echo $config->HasRosterTitle() ?  " - ".$config->GetRosterTitle() : null; ?></title>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
		<link rel="stylesheet" media="screen" type="text/css" href="styles/_.css" />
		<link rel="stylesheet" media="screen" type="text/css" href="styles/install.css" />
		<script type="text/javascript" src="js/overlib.js"><!-- overLIB (c) Erik Bosrup --></script>
		<script type="text/javascript">
			function CheckForm() {
				if (document.getElementById('adminPassword').value == '')
				{
					alert('<?php echo $tooltip->Flatten($localization->Get('you must enter an admin password')); ?>');
					return false;
				}
				
				if (document.getElementById('sqlServer').value == '')
				{
					alert('<?php echo $tooltip->Flatten($localization->Get('you must enter the name of the database server')); ?>');
					return false;
				}
				
				if (document.getElementById('sqlBase').value == '')
				{
					alert('<?php echo $tooltip->Flatten($localization->Get('you must enter the name of the database')); ?>');
					return false;
				}
				
				if (document.getElementById('sqlUser').value == '')
				{
					alert('<?php echo $tooltip->Flatten($localization->Get('you must enter the name of the database user')); ?>');
					return false;
				}
				
				if (document.getElementById('sqlPass').value == '')
				{
					if (confirm('<?php echo $tooltip->Flatten($localization->Get('you did not enter a password for the database user do you want to continue')); ?>') == false)
					{
						return false;
					}
				}
				
				if (document.getElementById('rosterTitle').value == '')
				{
					if (confirm('<?php echo $tooltip->Flatten($localization->Get('the default title will be used continue')); ?>') == false)
					{
						return false;
					}
				}
				
				if (document.getElementById('rosterGuild').value == '')
				{
					alert('<?php echo $tooltip->Flatten($localization->Get('you must type in the name of the guild')); ?>');
					return false;
				}
				
				if (document.getElementById('rosterServer').value == '')
				{
					alert('<?php echo $tooltip->Flatten($localization->Get('you must type in the name of the server where the guild is playing')); ?>');
					return false;
				}
				
				if (document.getElementById('rosterPass').value == '')
				{
					alert('<?php echo $tooltip->Flatten($localization->Get('no password has been configured, you must type one')); ?>');
					return false;
				}
				
				if (document.getElementById('sqlCreate').checked)
				{
					return confirm('<?php echo $tooltip->Flatten($localization->Get('this script will delete all existing data (if there are some) are you sure you want to continue')); ?>');
				}
				
				return true;
			}
		</script>
	</head>
	<body>
		
		<?php require_once "inc/head.php"; ?>
		
		<h2><?php echo $localization->Get('install'); ?></h2>
		
		<?php
		if ( ( isset($status_roster) and $status_roster->HasError() ) or $session_roster->HasStatus())
		{
		?>
			<p>
				<?php echo $localization->Get('this page produced some messages. please refer to the following lines'); ?>
			</p>
			
			<?php echo isset($status_roster) ?  $status_roster->GetStack_InList() : $session_roster->GetStatus()->GetStack_InList(); ?>
			
			<p>
				<?php echo $localization->Get('you can refresh the page %s and try again', "install.php"); ?>
			</p>
		<?php
		}
		else
		{
		?>
		
		<p>
			<?php echo $localization->Get('please fill in the form to configure the roster. you can reset the form (...)', "?reset"); ?>
		</p>
		
		<form action="install.php" method="post" onsubmit="return CheckForm();" class="send">
		<p style="font-size: 1.2em;">
			<strong><?php echo $localization->Get('database'); ?></strong>
		</p>
		<p>
			<?php echo $localization->Get('enter the appropriate information for the mysql server connection'); ?>:
		</p>
		<p>
			<label for="sqlServer">
				<?php echo $localization->Get('server'); ?>:
			</label>
			<input
				type="text"
				name="sqlServer"
				value="<?php echo $session_roster->GetVar('sqlServer') != null ? $session_roster->GetVar('sqlServer') : $config->GetSqlServer() ; ?>"
				id="sqlServer"
				onmouseover="return overlib('<?php echo $tooltip->Flatten($localization->Get('this value is generally localhost')); ?>', CAPTION, '<?php echo $tooltip->Flatten($localization->Get('server')); ?>');"
				onmouseout="return nd();"
			/><br />
			
			<label for="sqlBase">
				<?php echo $localization->Get('database'); ?>:
			</label>
			<input
				type="text"
				name="sqlBase"
				value="<?php echo $session_roster->GetVar('sqlBase') != null ? $session_roster->GetVar('sqlBase') : $config->GetSqlDatabase() ; ?>"
				id="sqlBase"
			/><br />
			
			<label for="sqlUser">
				<?php echo $localization->Get('user'); ?>:
			</label>
			<input
				type="text"
				name="sqlUser"
				value="<?php echo $session_roster->GetVar('sqlUser') != null ? $session_roster->GetVar('sqlUser') : $config->GetSqlUser() ; ?>"
				id="sqlUser"
			/><br />
			
			<label for="sqlPass">
				<?php echo $localization->Get('password'); ?>:
			</label>
			<input
				type="password"
				name="sqlPass"
				id="sqlPass"
			/><br />
			
			<label for="sqlCreate">
				<?php echo $localization->Get('create/recreate tables'); ?>:
			</label>
			<input
				type="checkbox"
				name="sqlCreate"
				
				<?php
				if ($session_roster->GetVar('sqlCreate') != null or $revision->GetInstalledRevision() == 0)
				{
				?>
				
				checked="checked"
				
				<?php
				}
				
				if ($revision->GetInstalledRevision() == 0)
				{
				?>
				
				disabled="disabled"
				
				<?php
				}
				?>
				
				id="sqlCreate"
				onmouseover="return overlib('<?php echo $tooltip->Flatten($localization->Get('check this box if you want to recreate the tables in the database (...)')); ?>', CAPTION, '<?php echo $tooltip->Flatten($localization->Get('create/recreate tables')); ?>', WIDTH, 250);"
				onmouseout="return nd();"
			/><br />
		</p>
		
		<p style="font-size: 1.2em;">
			<strong><?php echo $localization->Get('roster'); ?></strong>
		</p>
		<p>
			<?php echo $localization->Get('this part lets you set up the roster like you want'); ?>
		</p>
		
		<p>
			<label for="adminPassword">
				<?php echo $localization->Get('admin password'); ?>:
			</label>
			<input
				type="password"
				name="adminPassword"
				id="adminPassword"
				onmouseover="return overlib('<?php echo $tooltip->Flatten($localization->Get('this password is used to get access to the administration of the roster')); ?>', CAPTION, '<?php echo $tooltip->Flatten($localization->Get('admin password')); ?>', WIDTH, 250);"
				onmouseout="return nd();"
			/>
		</p>
		<p>
			<label for="rosterTitle">
				<?php echo $localization->Get('roster title'); ?>:
			</label>
			<input
				type="text"
				name="rosterTitle"
				value="<?php echo $session_roster->GetVar('rosterTitle') != null ? $session_roster->GetVar('rosterTitle') : $config->GetRosterTitle() ; ?>"
				id="rosterTitle"
				onmouseover="return overlib('<?php echo $tooltip->Flatten($localization->Get('this title is displayed in the browser title bar')); ?>', CAPTION, '<?php echo $tooltip->Flatten($localization->Get('roster title')); ?>');"
				onmouseout="return nd();"
			/><br />
			
			<label for="rosterGuild">
				<?php echo $localization->Get('guild names'); ?>:
			</label>
			<input
				type="text"
				name="rosterGuild"
				value="<?php echo $session_roster->GetVar('rosterGuild') != null ? $session_roster->GetVar('rosterGuild') : implode(", ", $config->GetRosterGuilds()) ; ?>"
				id="rosterGuild"
				onmouseover="return overlib('<?php echo $tooltip->Flatten($localization->Get('names of the guilds that use this roster. the names must be (...)')); ?>', CAPTION, '<?php echo $tooltip->Flatten($localization->Get('guild names')); ?>', WIDTH, 250);"
				onmouseout="return nd();"
			/><br />
			
			<label for="rosterServer">
				<?php echo $localization->Get('server'); ?>:
			</label>
			<input
				type="text"
				name="rosterServer"
				value="<?php echo $session_roster->GetVar('rosterServer') != null ? $session_roster->GetVar('rosterServer') : $config->GetRosterServer() ; ?>"
				id="rosterServer"
				onmouseover="return overlib('<?php echo $tooltip->Flatten($localization->Get('name of the server where the guild plays. be careful to (...)')); ?>', CAPTION, '<?php echo $tooltip->Flatten($localization->Get('server')); ?>', WIDTH, 250);"
				onmouseout="return nd();"
			/><br />
			
			<label for="rosterPass">
				<?php echo $localization->Get('user password'); ?>:
			</label>
			<input
				type="password"
				name="rosterPass"
				id="rosterPass"
				onmouseover="return overlib('<?php echo $tooltip->Flatten($localization->Get('this password will be asked each time someone tries to create an account')); ?>', CAPTION, '<?php echo $tooltip->Flatten($localization->Get('user password')); ?>', WIDTH, 250);"
				onmouseout="return nd();"
			/>
		</p>
		<p>
			<label for="rosterLanguage">
				<?php echo $localization->Get('default language'); ?>:
			</label>
			<select
				name="rosterLanguage"
				id="rosterLanguage"
				onmouseover="return overlib('<?php echo $tooltip->Flatten($localization->Get('this language will be used by default for not connected users')); ?>', CAPTION, '<?php echo $tooltip->Flatten($localization->Get('default language')); ?>', WIDTH, 250);"
				onmouseout="return nd();"
			>
				
				<?php
				foreach ($locale_container->GetAvailableLocales() as $locale)
				{
				?>
				
				<option value="<?php echo $locale->GetId(); ?>"
				
				<?php
				if ( $locale->GetId() == ($session_roster->GetVar('rosterLanguage') != null ? $session_roster->GetVar('rosterLanguage') : $config->GetLocalization()) )
				{
				?>
					selected="selected"
				<?php
				}
				?>
				
				
				><?php echo $locale->GetName(); ?></option>
				
				<?php
				}
				?>
				
			</select>
		</p>
		<p>
			<label for="rosterBanner">
				<?php echo $localization->Get('banner'); ?>:
			</label>
			<input
				type="text"
				name="rosterBanner"
				value="<?php echo $session_roster->GetVar('rosterBanner') != null ? $session_roster->GetVar('rosterBanner') : $config->GetRosterBanner() ; ?>"
				id="rosterBanner"
				onmouseover="return overlib('<?php echo $tooltip->Flatten($localization->Get('customizes the roster banner. your banner must be (...)')); ?>', CAPTION, '<?php echo $tooltip->Flatten($localization->Get('banner')); ?>', WIDTH, 250);"
				onmouseout="return nd();"
			/>
		</p>
		<p>
			<label for="rosterArmory" >
				<?php echo $localization->Get('armory'); ?>:
			</label>
			<select
				name="rosterArmory"
				id="rosterArmory"
				onmouseover="return overlib('<?php echo $tooltip->Flatten($localization->Get('link to the armory')); ?>', CAPTION, '<?php echo $tooltip->Flatten($localization->Get('armory')); ?>', WIDTH, 250);"
				onmouseout="return nd();"
			>
				
				<?php
				foreach($site_container->GetWoWArmories() as $site)
				{
				?>
				
				<option
					value="<?php echo $site->GetUrl(); ?>"
					
					<?php
					if ( $site->GetUrl() == ($session_roster->GetVar('rosterArmory') != null ? $session_roster->GetVar('rosterArmory') : $config->GetWoWArmory()) )
					{
					?>
						selected="selected"
					<?php
					}
					?>
					
				>
					<?php echo $site->GetDescription(); ?>
				</option>
				
				<?php
				}
				?>
				
			</select><br />
			
			<label for="rosterItemDatabase">
				<?php echo $localization->Get('items database'); ?>:
			</label>
			<select
				name="rosterItemDatabase"
				id="rosterItemDatabase"
				onmouseover="return overlib('<?php echo $tooltip->Flatten($localization->Get('link to a wow item database')); ?>', CAPTION, '<?php echo $tooltip->Flatten($localization->Get('items database')); ?>', WIDTH, 250);"
				onmouseout="return nd();"
			>
				
				<?php
				foreach($site_container->GetItemsDatabases() as $site)
				{
				?>
				
				<option
					value="<?php echo $site->GetUrl(); ?>"
					
					<?php
					if ( $site->GetUrl() == ($session_roster->GetVar('rosterItemDatabase') != null ? $session_roster->GetVar('rosterItemDatabase') : $config->GetItemDatabase()) )
					{
					?>
						selected="selected"
					<?php
					}
					?>
					
				>
					<?php echo $site->GetDescription(); ?>
				</option>
				
				<?php
				}
				?>
				
			</select><br />
			
			<label for="rosterSpellDatabase">
				<?php echo $localization->Get('spells database'); ?>:
			</label>
			<select
				name="rosterSpellDatabase"
				id="rosterSpellDatabase"
				onmouseover="return overlib('<?php echo $tooltip->Flatten($localization->Get('link to a wow spell database')); ?>', CAPTION, '<?php echo $tooltip->Flatten($localization->Get('spells database')); ?>', WIDTH, 250);"
				onmouseout="return nd();"
			>
				
				<?php
				foreach($site_container->GetSpellsDatabases() as $site)
				{
				?>
				
				<option
					value="<?php echo $site->GetUrl(); ?>"
					
					<?php
					if ( $site->GetUrl() == ($session_roster->GetVar('rosterSpellDatabase') != null ? $session_roster->GetVar('rosterSpellDatabase') : $config->GetSpellDatabase()) )
					{
					?>
						selected="selected"
					<?php
					}
					?>
					
				>
					<?php echo $site->GetDescription(); ?>
				</option>
				
				<?php
				}
				?>
				
			</select><br />
		</p>
		
		<p style="font-size: 1.2em;">
			<strong><?php echo $localization->Get('send'); ?></strong>
		</p>
		<p>
			<?php echo $localization->Get('please check all info before proceding'); ?>.
		</p>
		<p>
			<label for="formSubmit">
				<?php echo $localization->Get('install the roster'); ?>:
			</label>
			<input type="submit" name="install" value="<?php echo $localization->Get('install'); ?>" id="formSubmit" />
		</p>
		</form>
		
		<?php
		}
		?>
		
		<?php require_once "inc/foot.php"; ?>
		
	</body>
</html>